﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;
using System.Collections;

namespace BLL
{
    public class NienKhoaBLL
    {
        static int id=0;
        QuanLyDiemDataContext DB = new QuanLyDiemDataContext();
        public IQueryable SelectDes()
        {
            return DB.NienKhoas.OrderByDescending(s => s.MaNienKhoa).AsQueryable();
        }
        public string Insert(NienKhoaDTO nienkhoa)
        {
            try
            {
                NienKhoa nk = new NienKhoa();
                nk.MaNienKhoa = (id++).ToString();
                nk.TenNienKhoa = nienkhoa.TenNienKhoa;
                nk.TrangThai = nienkhoa.TrangThai;
                DB.NienKhoas.InsertOnSubmit(nk);
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public string Update(NienKhoaDTO nienkhoa)
        {
            try
            {
                NienKhoa nk = DB.NienKhoas.Where(s => s.MaNienKhoa == nienkhoa.Manienkhoa).FirstOrDefault();
                nk.TenNienKhoa = nienkhoa.TenNienKhoa;
                nk.TrangThai = nienkhoa.TrangThai;
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public string Delete()
        {
            try
            {                
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public bool check(string tennienkhoa)
        {
            var kq = DB.NienKhoas.Where(n => n.TenNienKhoa == tennienkhoa).ToList();
            int kt = 0;
            kt = kq.Count();
            if (kt == 0)
                return true;
            else return false;
        }

       
    }
}
